package o2o.config.init;

import com.danga.MemCached.MemCachedClient;
import com.jinyou.utils.common.DateUtils;
import o2o.area.ChnAreaMemory;
import o2o.cache.agent.*;
import o2o.cache.sys.SysConfigXcxPushMemory;
import o2o.cache.area.CityMemory;
import o2o.cache.area.CountyMemory;
import o2o.cache.area.ProvinceMemory;
import o2o.cache.area.RegionInfoMemory;
import o2o.cache.comment.OrderCommentMemory;
import o2o.cache.company.SysCityCompanyMemory;
import o2o.cache.company.SysCityCompanyUserRelationMemory;
import o2o.cache.company.postman.*;
import o2o.cache.divide.CityAgentFeeMemory;
import o2o.cache.divide.DividingInfoMemory;
import o2o.cache.home.HomeRecommendMemory;
import o2o.cache.order.*;
import o2o.cache.postman.*;
import o2o.cache.print.PrintConfigMemory;
import o2o.cache.print.PrintMessageMemory;
import o2o.cache.prompt.PromptsInfoMemory;
import o2o.cache.shop.*;
import o2o.cache.sys.*;
import o2o.cache.system.SysConfigPathMemory;
import o2o.cache.system.SysSettingMemory;
import o2o.cache.system.SysSettingMobileMemory;
import o2o.cache.tip.TipImageConfigMemory;
import o2o.cache.tip.TipInfoMemory;
import o2o.cache.tip.TipInfoPaotuiMemory;
import o2o.cache.user.*;
import o2o.cache.wallet.UserWalletFlowMemory;
import o2o.cache.wallet.UserWalletMemory;
import o2o.config.jms.producer.QueueMessageProducer;
import o2o.config.jms.producer.TopicMessageProducer;
import o2o.config.properties.*;
import o2o.system.SysConfigPath;
import o2o.system.SysSettings;
import o2o.system.data.SYS_SETTINGS;
import o2o.task.image.ImageService;
import o2o.web.actions.order.VerifyCodeMan;
import o2o.web.actions.order.thread.CancelOrder4ShopThread;
import o2o.web.actions.order.thread.OrderThread;
import o2o.web.actions.order.thread.OverOrderThread;
import o2o.web.actions.pay.PayThreads;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import pay.config.PayConfig;
import version.SysAppVersionMemory;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author Jahnke【1029777564@qq.com】
 * @ClassName AutoWiredClass
 * @Description:
 * @date 2020/4/29 9:26
 * @Version 1.0
 */
@Service
@DependsOn("redisManager")
public class AutoWiredClass {

    @Autowired
    private MemCachedClient client;
    public static MemCachedClient client2;

    @Resource(name = "cloudPrimarySource")
    private DataSource userSource;
    public static DataSource userSource2;

    @Resource(name = "cloudSecondSource")
    private DataSource dataSource;
    public static DataSource dataSource2;

    @Autowired
    private ConfigProperties configProperties;
    public static ConfigProperties configProperties2;
    @Autowired
    private TopicMessageProducer topicMessageProducer;
    public static TopicMessageProducer topicMessageProducer2;
    @Autowired
    private QueueMessageProducer queueMessageProducer;
    public static QueueMessageProducer queueMessageProducer2;


    @Autowired
    private OssProperties ossProperties;
    public static OssProperties ossProperties2;

    @Autowired
    private MongoProperties mongoProperties;
    public static MongoProperties mongoProperties2;

    @PostConstruct
    public void init() {
        client2 = client;
        userSource2 = userSource;
        dataSource2 = dataSource;
        configProperties2 = configProperties;
        topicMessageProducer2 = topicMessageProducer;
        queueMessageProducer2 = queueMessageProducer;
        mongoProperties2 = mongoProperties;
        ossProperties2 = ossProperties;

        try {
            this.initCache();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        this.threadRun();
    }

    public void initCache() {
        SysCustomerCache.getInstance().load(); //加载系统客户列表

        SysConfigPath.init();
        SysSettings.init();


        Long time1 = DateUtils.getCurrTime();
        loadMemory();
        Long time2 = DateUtils.getCurrTime();
        System.out.println("缓存加载完成，用时：" + (time2 - time1));

        //初始化支付相关
        PayConfig.init();
//        PAY_CONFIG.init();//支付返回初始化
    }

    private void threadRun() {
        System.out.println("启动线程开始......");

        PayThreads.getInstance().start();
        OrderThread.getInstance().start();
        OverOrderThread.getInstance().start();//订单超时未完成，自动完成
        if (null != SYS_SETTINGS.ORDER_AUTO_CANCEL_TIME && SYS_SETTINGS.ORDER_AUTO_CANCEL_TIME > 0) {
            CancelOrder4ShopThread.getInstance().start();//超时未接单，自动取消订单
        }

        System.out.println("启动线程结束......");
    }

    public static void loadMemory() {

        System.out.println("项目启动加载缓存.....");

        VerifyCodeMan.getInstance().run(); // 验证码库加载


        SysSettingMobileMemory.getInstance().run();//加载手机端系统配置

        SysSettingMemory.getInstance().run(); //加载系统配置
        SysConfigPathMemory.getInstance().load2Memory(); //加载系统路径配置
//        SysAppVersionMemory.getInstance().run(); //加载版本更新

        PromptsInfoMemory.getInstance().run(); // 提示信息多语言

        SysConfigPayMemory.getInstance().run(); //支付信息相关
        SysConfigSmsCache.getInstance().run(); //短信配置相关

        UserPwdMemory.getInstance().run();   //加载密码信息
        UserInfoMemory.getInstance().run();   //加载用户信息
        UserAddressMemory.getInstance().run();//加载用户收货地址缓存
        UserInfoExtraMemory.getInstance().run();//加载用户额外信息缓存

        UserAddressRecordMemory.getInstance().run();  // 用户收货地址历史记录


        CityAgentMemory.getInstance().run(); //加载代理（学校）
        AgentManagerMemory.getInstance().run(); // 加载区域代理
        CityAgentAddressMemory.getInstance().run(); //代理下的地址列表
        ShopAgentRelationMemory.getInstance().run(); //加载代理下的店铺（学校）
        CityAgentFeeMemory.getInstance().run(); // 时间段设置配送费

        ShopMemory.getInstance().run();//加载店铺信息
        ShopExtraInfoMemory.getInstance().run();//加载店铺的额外信息（订单量，评分，星级）
        ShopStatisticsPreDayMemory.getInstance().run();//加载每日店铺订单量

        ShopImageTypeMemory.getInstance().run();//加载店铺实景
        ShopLikeMemory.getInstance().run();//加载店铺收藏

        PostManLikeMemory.getInstance().run(); // 加载用户收藏的骑手


        ShopTipMemory.getInstance().load();//加载店铺标签
        ShopTipRelationMemory.getInstance().load();//加载店铺标签关系
        PostmanOnlineMemory.getInstance().reLoad();// 骑手在线情况

//        ShopWalletMemory.getInstance().run();//加载商铺钱包
//        ShopWalletPopMemory.getInstance().run();//加载商铺钱包提现金额

        //钱包相关
//        UserWalletMemory.getInstance().run();//用户钱包
        UserWalletFlowMemory.getInstance().run();//用户钱包

        // 分成信息
        DividingInfoMemory.getInstance().run(); // 分成信息


//        SysAreaMemory.getInstance().run();//加载所有省市区

        // 省
        ProvinceMemory.getInstance().run();
        // 市
//        CityMemory.getInstance().run();
        // 区
        CountyMemory.getInstance().run();

        ChnAreaMemory.getInstance().run();//加载所有的省市区

        RegionInfoMemory.getInstance().run();//加载所有的商圈



        ShopGoodsRelationMemory.getInstance().run(); //

        //订单信息
        OrderStatusNameMemory.getInstance().run();//名称
        OrderInfoMemory.getInstance().run();//加载所有的订单信息
        OrderInfoPreMemory.getInstance().run();//加载所有的订单信息
        OrderGoodsMemory.getInstance().run();//加载订单对应商品
        OrderShopSpecsMemory.getInstance().run();//订单所选店铺属性
        OrderStatusMemory.getInstance().run();//加载订单状态
        OrderPayMemory.getInstance().run();//订单支付流水
        OrderVerifyMemory.getInstance().run();//加载订单验证记录
        OrderGameMemory.getInstance().run();//订单的活动信息
        OrderGoodsLabelMemory.getInstance().run();//订单标签缓存
        OrderGoodsLabelPreMemory.getInstance().run();//订单标签缓存
        OrderItemsMemory.getInstance().run();//订单项目缓存
        OrderInfoImageMemory.getInstance().run();//订单图片
        OrderInfoExtraMemory.getInstance().run();//订单额外信息
        OtherOrderMemory.getInstance().run();//加载跑腿订单

        OrderTypeMemory.getInstance().run(); // 加载订单类型
        OrderPolicyRecordMemory.getInstance().run();    // 加载订单投保记录
        OrderCommentMemory.getInstance().run(); // 订单评论信息

        OrderCancelGoodsMemory.getInstance().run(); // 取消订单商品信息
        OrderCancelInfoMemory.getInstance().run();  // 取消订单信息

        AgentOrderOverTimeMemory.getInstance().run(); // 商圈订单超时信息设置

        OrderRedPacketRecordMemory.getInstance().run();//订单使用的红包信息

        //管理端
//        CityPostmanMemory.getInstance().run();//加载城市配送员
        SysConfigJPushCache.getInstance().run();//加载推送配置

        ShopManagerRelationMemory.getInstance().load2Memory();//店铺管理员（业务员、区域代理关联）

        ShopRegionRelationMemory.getInstance().run();//店铺对应商圈
        UserAgentRelationMemory.getInstance().load2Memory();  //商圈  代理下的商圈
        PostmanAgentRelationMemory.getInstance().run();//配送员  代理下的配送员

        //卡卡外卖跑腿
        SysVariablePaotuiMemory.getInstance().run(); //跑腿变量加载
        SysVariableScoreLevelMemory.getInstance().run(); //积分等级加载
        SysVariableDistributeMemory.getInstance().run(); //分成加载


        //加载配送员位置
        PostmanPositionMemory.getInstance().load2Memory();
        //配送员订单异常备选项
        PostmanTroubleOptionMemory.getInstance().load2Memory();

        //加载图片配置信息
        TipImageConfigMemory.getInstance().run();
        //加载提示信息配置
        TipInfoMemory.getInstance().run();
        TipInfoPaotuiMemory.getInstance().run();


        //------------其他配置
        //SysConfigDepositMemory.getInstance().load2Memory(); //押金

        // 骑手服务店铺关系加载
        PostmanShopRelationMemory.getInstance().run();

        //
        PrintConfigMemory.getInstance().run(); // 配置信息
        PrintMessageMemory.getInstance().run();  // 打印机

        //权限模块
        SysCityCompanyMemory.getInstance().run(); // 公司信息
        SysCityCompanyUserRelationMemory.getInstance().run(); // 公司员工关联信息

        HomeRecommendMemory.getInstance().run();    // 首页推荐
        // 公司模块
        PostTypeMemory.getInstance().run(); // 配送类型
        PostCompanyMemory.getInstance().run(); // 配送公司
        CompanyTypeRelationMemory.getInstance().run(); // 公司类型
        PostmanTypeRelationMemory.getInstance().run(); // 骑手类型
        PostmanSignCompanyMemory.getInstance().run(); // 公司骑手签约


        // 小程序推送配置
        SysConfigXcxPushMemory.getInstance().run();

        System.out.println("项目缓存加载完成");
    }

}
